<?php
$this->headScript()->appendFile($this->js('plugins/jquery.jqprint.js'))?>
<script>
$(function(){
    $("#yearSemester, #select-class").change(function(){
        $("#selectForm").submit();
     });
    $("input[name='listMode']").click(function(){
          if ($("#select-class").val() == '') return false;
          $("#selectForm").submit();
    });
    $("#friendlyPrint").click(function(){
    	$("#scoreTable").append('<caption>'+$("#yearSemester option:selected").text()+
    		    $("#select-class option:selected").text() + '<?php echo $this->translate('semester score list')?>'+
    	    	'</caption>');
        $("#scoreTable").addClass('printTable').jqprint();
      });
    $("#exportCvs").click(function(){
        $("#act").val('exportCvs');
        $("#selectForm").submit();
        $("#act").val('');
      });
    $("#scoreTable tbody tr:even").addClass('even');
    $("#scoreTable tbody tr:odd").addClass('odd');
});
</script>
<style>
.printTable {border:#000 thine solid;}
.printTable td {border:#000 thine solid;}
#scoreTable {background: #ccc; text-align:center; border-spacing: 1px; font-size:14px;margin: 5px  auto; width:100%}
#scoreTable caption {font-size:16px; font-weight: bold;}
#scoreTable th {width:60px;}
#scoreTable td {padding:3px;}
#scoreTable thead tr {background: #bef}
.odd {background: #ffe}
.even {background: #efe}

</style>
<div class="ui-widget">
<form action="" method="post" id="selectForm">
<select name="yearSemester" id="yearSemester">
<?php echo $this->YearSemesterOptions($this->yearSemester,array('order'=>'year DESC'))?>
</select>
<select id="select-class" name="select-class">
<option value=""><?php echo $this->translate('choose class')?></option>
<?php echo $this->ClassNameOptions($this->yearSemester , $this->selectClass)?>
</select>
<?php if ($this->selectClass):?>
<?php if ($this->scoreData) :?>
<input type="button" id="friendlyPrint" value="<?php echo $this->translate('friendly print')?>"/>
<input type="button" id="exportCvs" value="<?php echo $this->translate('export to cvs')?>"/>
<?php endif?>
 <table id="scoreTable">
<thead>
<tr>
<th rowspan="2"><?php echo $this->translate('number')?></th>
<th rowspan="2"><?php echo $this->translate('student id')?></th>
<th rowspan="2" ><?php echo $this->translate('student name')?></th>
<?php foreach($this->semestetSubjectArr as $scopeName=>$val):?>
<?php if (count($val) >1):?>
<?php $flag = true;?>
<th colspan="<?php echo count($val)+1?>"><?php echo $scopeName?></th>
<?php else:?>
<th rowspan="2"><?php echo $val[0]['name']?>
<br/><span class="rate">(<?php echo $this->subjectRate[$val[0]['subject_sn']]?>)</span>
</th>
<?php endif;?>
<?php endforeach?>
<th rowspan="2"><?php echo $this->translate('total average')?></th>
<th rowspan="2"><?php echo $this->translate('judgement')?></th>
</tr>
<?php if ($flag):?>
<tr>
<?php foreach($this->semestetSubjectArr as $val):?>
<?php if (count($val) > 1):?>
<?php foreach($val as $row):?>
<th><?php echo $row['name']?>
<br/><span class="rate">(<?php echo $this->subjectRate[$row['subject_sn']]?>)</span>
</th>
<?php endforeach?>
<th><?php echo $this->translate('average')?></th>
<?php endif?>
<?php endforeach?>
</tr>
<?php endif?>
</thead>
<?php if ($this->scoreData) :?>
<tbody>

<?php foreach($this->scoreData as $row):?>
<?php
 $arr = array();
$totalScore = $totalAvgCount = 0;
foreach($row['Student_Model_StudentScoreSemester'] as $val)
$arr[$val['subject_sn']] = $val['score'];
?>
<tr>
<td><?php echo $row['number']?></td>
<td><?php echo $row['student_id']?></td>
<td style="white-space: nowrap;"><?php echo $row['name']?></td>

<?php foreach ($this->semestetSubjectArr as $subArr):?>
<?php $sum = $avgCount = 0 ;?>
<?php foreach ($subArr as $subjectData):?>
<td>
<?php
 if(isset($arr[$subjectData['subject_sn']]))  {
     $sum += $arr[$subjectData['subject_sn']] * $this->subjectRate[$subjectData['subject_sn']];
     $avgCount += $this->subjectRate[$subjectData['subject_sn']];
    echo $arr[$subjectData['subject_sn']];
    $totalScore += $sum;
    $totalAvgCount += $avgCount;
 }
  else echo '-';?>
  </td>
<?php endforeach?>
<?php if (count($subArr) >1) :?>
<td><?php if ($avgCount>0) echo round($sum/$avgCount,2) ?></td>
<?php endif?>
<?php endforeach?>
<td>
<?php if ($totalAvgCount > 0) {$totalAverage = $totalScore/$totalAvgCount; echo round($totalAverage,2) ;}?></td>
<td><?php if (isset($totalAverage)) echo Semester_Model_SemesterExamine::resolveJudgement($totalAverage, $this->judgement)?></td>
</tr>
<?php endforeach?>

</tbody>
<?php endif?>
</table>


<?php endif ?>
<input type="hidden" id="act" name="act" />
</form>

</div>
